package game.gameobject.misc;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.control.LightControl;
import com.jme3.scene.shape.Box;
import game.DataStore;

public class Torch extends Node {
    protected float width, height, mass, lightRadius;
    protected Geometry base;
    protected Material material;
    protected ParticleEmitter effect;
    protected PointLight light;

    /** Creates a torch standing straight up from bottomPoint. */
    public Torch() {
	super("torch");
	
	initDefaultValues();
	createTorch();
    }

    private void initDefaultValues() {
	width = 0.2f;
	height = 0.5f;
	mass = 0.5f;
	lightRadius = 10f;
    }

    private void createTorch() {
	createTorchBase();
	createEffect();
	createLight();
    }

    private void createTorchBase() {
	Box boxShape = new Box(width/2, height/2, width/2);
	base = new Geometry("torchBase", boxShape);
	material = new Material(DataStore.assetManager, "Common/MatDefs/Light/Lighting.j3md");
	material.setBoolean("UseMaterialColors",true);
	material.setColor("Diffuse",  ColorRGBA.White);
        material.setColor("Specular", ColorRGBA.White);
        base.setMaterial(material);
	this.attachChild(base);
    }

    private void createEffect() {
	/** Uses Texture from jme3-test-data library! */
	effect = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
	Material mat_red = new Material(DataStore.assetManager, "Common/MatDefs/Misc/Particle.j3md");
	mat_red.setTexture("Texture", DataStore.assetManager.loadTexture("Effects/Explosion/flame.png"));
	effect.setMaterial(mat_red);
	effect.setLocalTranslation(0f, height/2, 0f);
	effect.setImagesX(2);
	effect.setImagesY(2);
	effect.setEndColor(new ColorRGBA(1f, 0f, 0f, 1f));
	effect.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f));
	effect.getParticleInfluencer().setInitialVelocity(new Vector3f(0f, 1f, 0f));
	effect.setStartSize(0.2f);
	effect.setEndSize(0.05f);
	effect.setLowLife(0.5f);
	effect.setHighLife(1f);
	effect.getParticleInfluencer().setVelocityVariation(0.3f);
	this.attachChild(effect);

    }

    private void createLight() {
	/** A white, spot light source. */
	light = new PointLight();
	light.setColor(ColorRGBA.White);
	light.setRadius(lightRadius);
	DataStore.rootNode.addLight(light); //Lights only affect things under the node they're added to

	LightControl lightControl = new LightControl(light);
	this.addControl(lightControl);
    }

    public void createPhysicsControl() {
	RigidBodyControl torchControl = new RigidBodyControl(new BoxCollisionShape(new Vector3f(width/2, height/2, width/2)), mass);
        this.addControl(torchControl);
    }

    public float getWidth() {
	return width;
    }
    
    public float getHeight() {
	return height;
    }

    public PointLight getLight() {
	return light;
    }
}
